// we7/pages/index/XSMSIntegralExchange.js
var dateTimePicker = require('../../resource/utils/dateTimePicker.js');
const app = getApp();
Page({ 
 
  /**
   * 页面的初始数据
   */
  data: {
    // singleOrder:"",
    goods_image: "",
    goods_name: "",
    goods_num: "",
    goods_present_price:"",
    specification_name:"",
    total_price:"",
    ziList:true,
    sele_select:true,
    sele_selectp:false,
    StatusBar: app.globalData.StatusBar, //模态动画
    CustomBar: app.globalData.CustomBar,
    ziTi:true,
    peiSong:false,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2000,
    endYear: 2030,
    moRenShop:"",
    shop_id:"",
    order_remark:"",
    order_phone:"",
    self_taking_name:'',
    peiSongStyle:2,
    choose_shou:true, 
    address_id:"",
    addressName: "",
    addressPhone:"",
      addressDetail:"",
      address_tiao:false,
    shopcarOrder:"",
    shopTotalPrice:"",
    shopcar_id:""
  },
  ziTi:function(){
    var that=this
    that.setData({
      ziList: true,
      sele_select:true,
      sele_selectp: false,
      peiSongStyle: 2
    })
  },
  peiSong:function(){
    var that = this
    that.setData({
      ziList: false,
      sele_selectp: true,
      sele_select: false,
      peiSongStyle:1
    })
  },
  //模态动画
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // 客服电话
  dailService(e){
    var that=this
    that.hideModal(e)
    wx.makePhoneCall({
      phoneNumber: '400-988-888' // 仅为示例，并非真实的电话号码
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 默认门店
    const value = wx.getStorageSync('openid');
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        app.util.request({
          url: 'admin/WxGoodsApi/nearby_store',
          data: {
            uid: value,
            longitude: latitude,
            latitude: longitude
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            if (res.data.code == 0) {
              var data = res.data.data;
              console.log(data);
              for (var c = 0; c < data.length; c++) {

                if (data[c].default_shop === 1) {
                  that.setData({
                    moRenShop: data[c].shop_name,
                    shop_id: data[c].id
                  })
                }
              }
              if (that.data.moRenShop == "") {
                that.setData({
                  moRenShop: data[0].shop_name,
                  shop_id: data[0].id
                })
                console.log(data[0].id);
              }
            }

          },
          fail: function (res) {
            wx.showToast({
              title: '服务器响应失败',
              icon: 'none',
              duration: 2000
            })
          }
        });
      }
    });
    // console.log(options)
    if (options.address_tiao){
      this.setData({
        address_tiao: options.address_tiao,
        address_id: options.address_id,
        addressName: options.addressName,
        addressPhone: options.addressPhone,
        addressDetail: options.addressDetail,
        choose_shou:false,
        sele_selectp: true

      })
    }
    if (this.data.sele_selectp){
      this.setData({
        ziList: false,
        sele_select: false,
        peiSongStyle: 1
      })
    }else{
      this.setData({
        ziList: true,
        sele_select: true,
        peiSongStyle: 2
      })
    }
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
    wx.setNavigationBarTitle({
      title: '订单确认',
    })
    

    if (options.shopcar_id){
      var that = this;
      var shopcar_id = options.shopcar_id
      that.setData({
        shopcar_id: shopcar_id
      })
      
      app.util.request({
        url: 'admin/WxGoodsApi/shopcarOrder',
        data: {
          shopcar_id: shopcar_id,
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          // console.log(res)
          // console.log(res.data.all_total_price)
          var data = res.data.data;
          
          for (var s = 0; s < data.length; s++) {
              data[s]['goods_image'] = app.globalData.url + data[s]['goods_image'];
            }
          // console.log(data[0].goods_image)
          if (res.data.code == 0) {
            that.setData({
              shopcarOrder: data,
              shopTotalPrice: res.data.all_total_price
            })
          }

        },
        fail: function (res) {
          wx.showToast({
            title: "服务器无响应",
            icon: 'none',
            duration: 2000
          })
        }

      });
    }else{
      var goods_id = options.goods_id
      var specification_id = options.specification_id
      var goods_num = options.goods_num
      var that = this;
      app.util.request({
        url: 'admin/WxGoodsApi/singleOrder',
        data: {
          goods_id: goods_id,
          goods_num: goods_num,
          specification_id: specification_id
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var data = res.data.data

          data['goods_image'] = app.globalData.url + data['goods_image'];
          if (res.data.code == 0) {
            that.setData({
              // singleOrder: data,
              goods_id: goods_id,
              specification_id: specification_id,
              goods_num: goods_num,
              goods_image: data.goods_image,
              goods_name: data.goods_name,
              goods_num: data.goods_num,
              goods_present_price: data.goods_present_price,
              specification_name: data.specification_name,
              total_price: data.total_price
            })
          }

        },
        fail: function (res) {
          wx.showToast({
            title: "服务器无响应",
            icon: 'none',
            duration: 2000
          })
        }

      });
    }
    
  },
  // order_remark 备注
  order_remark:function(e){
    var that=this
    that.setData({
      order_remark: e.detail.value
    })
  },
  self_taking_phone:function(e){
    var that = this
    that.setData({
      self_taking_phone: e.detail.value
    })
  },
  self_taking_name: function (e) {
    var that = this
    that.setData({
      self_taking_name: e.detail.value
    })
  },

  // 提交订单
  submitOrder:function(form){
    console.log(form.detail.formId)
    var formId = form.detail.formId
    var that = this;
    // 提交多个订单
    if (that.data.shopcar_id){
      var shopcar_id = that.data.shopcar_id
      const value = wx.getStorageSync('openid')
      // 自提时间
      var self_taking_time = that.data.dateTimeArray1[0][that.data.dateTime1[0]] + "-" + that.data.dateTimeArray1[1][that.data.dateTime1[1]] + "-" + that.data.dateTimeArray1[2][that.data.dateTime1[2]] + " " + that.data.dateTimeArray1[3][that.data.dateTime1[3]] + ":" + that.data.dateTimeArray1[4][that.data.dateTime1[4]]
      // 多个订单自提
      if (that.data.peiSongStyle == 2) {
        if (!that.data.self_taking_name) {
          wx.showToast({
            title: "您还未填写联系人哦",
            icon: 'none',
            duration: 2000
          })
          return false
        }
        if (!that.data.self_taking_phone) {
          wx.showToast({
            title: "您还未填写联系方式哦",
            icon: 'none',
            duration: 2000
          })
          return false
        }
        app.util.request({
          url: 'admin/WxGoodsApi/submitShopcarOrder',
          data: {
            shopcar_id: shopcar_id,
            uid: value,
            order_remark: that.data.order_remark,
            receiving_goods_type: that.data.peiSongStyle,
            self_taking_time: self_taking_time,
            self_taking_phone: that.data.self_taking_phone,
            self_taking_name: that.data.self_taking_name,
            shop_id: that.data.shop_id,
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res)
            if (res.data.code == 0) {
              app.startpay(res.data.data)
              // wx.showToast({
              //   title: res.data.msg,
              //   icon: 'none',
              //   duration: 2000
              // })
            }

          },
          fail: function (res) {
            wx.showToast({
              title: "服务器无响应",
              icon: 'none',
              duration: 2000
            })
          }

        });
      }
      // 多个订单配送
      if (that.data.peiSongStyle == 1) {
        if (that.data.choose_shou) {
          wx.showToast({
            title: "您还未选择收货地址哦",
            icon: 'none',
            duration: 2000
          })
          return false
        }
        app.util.request({
          url: 'admin/WxGoodsApi/submitShopcarOrder',
          data: {
            shopcar_id: shopcar_id,
            uid: value,
            shop_id: that.data.shop_id,
            order_remark: that.data.order_remark,
            receiving_goods_type: that.data.peiSongStyle,
            address_id: that.data.address_id,
            formId: formId
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            if (res.data.code == 0) {
              app.startpay(res.data.data)
              // wx.showToast({
              //   title: res.data.msg,
              //   icon: 'none',
              //   duration: 2000
              // })
            }

          },
          fail: function (res) {
            wx.showToast({
              title: "服务器无响应",
              icon: 'none',
              duration: 2000
            })
          }

        });
      }



    }else{
      // 单个提交自提
      var goods_id = that.data.goods_id
      var specification_id = that.data.specification_id
      var goods_num = that.data.goods_num
      const value = wx.getStorageSync('openid')
      // 自提时间
      var self_taking_time = that.data.dateTimeArray1[0][that.data.dateTime1[0]] + "-" + that.data.dateTimeArray1[1][that.data.dateTime1[1]] + "-" + that.data.dateTimeArray1[2][that.data.dateTime1[2]] + " " + that.data.dateTimeArray1[3][that.data.dateTime1[3]] + ":" + that.data.dateTimeArray1[4][that.data.dateTime1[4]]
      // 自提
      if (that.data.peiSongStyle == 2) {
        if (!that.data.self_taking_name) {
          wx.showToast({
            title: "您还未填写联系人哦",
            icon: 'none',
            duration: 2000
          })
          return false
        }
        if (!that.data.self_taking_phone) {
          wx.showToast({
            title: "您还未填写联系方式哦",
            icon: 'none',
            duration: 2000
          })
          return false
        }
        // if (!that.data.order_remark) {
        //   wx.showToast({
        //     title: "您还未填写备注哦",
        //     icon: 'none',
        //     duration: 2000
        //   })
        //   return false
        // }
        app.util.request({
          url: 'admin/WxGoodsApi/singleSubmitOrder',
          data: {
            goods_id: goods_id,
            goods_num: goods_num,
            specification_id: specification_id,
            uid: value,
            shop_id: that.data.shop_id,
            order_remark: that.data.order_remark,
            receiving_goods_type: that.data.peiSongStyle,
            self_taking_time: self_taking_time,
            self_taking_phone: that.data.self_taking_phone,
            self_taking_name: that.data.self_taking_name,
            formId: formId
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            
            if (res.data.code == 0) {
              app.startpay(res.data.data)
              // console.log(res.data.data)
              // wx.showToast({
              //   title: res.data.msg,
              //   icon: 'none',
              //   duration: 2000
              // })
            }

          },
          fail: function (res) {
            wx.showToast({
              title: "服务器无响应",
              icon: 'none',
              duration: 2000
            })
          }

        });
      }
      // 配送 
      if (that.data.peiSongStyle == 1) {
        if (that.data.choose_shou) {
          wx.showToast({
            title: "您还未选择收货地址哦",
            icon: 'none',
            duration: 2000
          })
          return false
        }
        // if (!that.data.order_remark) {
        //   wx.showToast({
        //     title: "您还未填写备注哦",
        //     icon: 'none',
        //     duration: 2000
        //   })
        //   return false
        // }
        app.util.request({
          url: 'admin/WxGoodsApi/singleSubmitOrder',
          data: {
            goods_id: goods_id,
            goods_num: goods_num,
            specification_id: specification_id,
            uid: value,
            shop_id: that.data.shop_id,
            order_remark: that.data.order_remark,
            receiving_goods_type: that.data.peiSongStyle,
            address_id: that.data.address_id,
            formId: formId
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res)
            if (res.data.code == 0) {
              app.startpay(res.data.data)
              console.log(res)
              // console.log(res.data.data.out_trade_no)
              // that.globalData.out_trade_no = res.data.data.out_trade_no;
            }

          },
          fail: function (res) {
            wx.showToast({
              title: "服务器无响应",
              icon: 'none',
              duration: 2000
            })
          }

        });
      }
    }
  },
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });

  },

  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})